﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using FlickrXplorer.Web.Models;
using FlickrXplorer.Web.Objects;

namespace FlickrXplorer.Web
{
  public partial class PhotoDataForm : Form
  {
    private PhotoData photoData;
    private int currentPhotoIndex;
    private PhotoModel photoModel;

    public PhotoDataForm()
    {
      InitializeComponent();
    }

    public PhotoDataForm(PhotoData photoData)
    {
      this.photoData = photoData;
      this.photoModel = new PhotoModel();
      InitializeComponent();
      InitialisePhotoData();
    }

    private void InitialisePhotoData()
    {
      this.Text = this.photoData.GetType().FullName;
      this.textBoxPhotos.Text = String.Format("{0} photos, {1} in this page.",
        this.photoData.Photos.Count,
        this.photoData.Total);
      this.currentPhotoIndex = 0;
      this.pictureBoxPhoto.LoadCompleted += new AsyncCompletedEventHandler(pictureBoxPhoto_LoadCompleted);
      this.pictureBoxPhoto.LoadProgressChanged += new ProgressChangedEventHandler(pictureBoxPhoto_LoadProgressChanged);
      UpdatePhotoPager();
    }

    private void UpdatePhotoPager()
    {
      this.labelProgress.Text = String.Format("Showing photo {0} of {1}.",
        this.currentPhotoIndex + 1,
        this.photoData.Photos.Count);
      this.progressBar1.Value = 0;
      this.progressBar1.Visible = true;
      this.labelTitle.Text = this.photoData.Photos[this.currentPhotoIndex].Title;
      this.pictureBoxPhoto.LoadAsync(this.photoData.Photos[this.currentPhotoIndex].Url);
    }

    private void buttonNext_Click(object sender, EventArgs e)
    {
      this.currentPhotoIndex += 1;
      if (this.currentPhotoIndex == this.photoData.Photos.Count)
      {
        this.currentPhotoIndex = 0;
      }
      UpdatePhotoPager();
    }

    void pictureBoxPhoto_LoadCompleted(object sender, AsyncCompletedEventArgs e)
    {
      this.progressBar1.Visible = false;
    }

    void pictureBoxPhoto_LoadProgressChanged(object sender, ProgressChangedEventArgs e)
    {
      this.progressBar1.Value = e.ProgressPercentage;
    }
  }
}
